﻿Imports System.IO

<System.ComponentModel.ToolboxItem(False)>
Public Class BasePictureItem
    Inherits System.Windows.Forms.PictureBox

    Friend WithEvents PictureBox As System.Windows.Forms.PictureBox
    Friend ReadOnly Property PictureView As PictureView
        Get
            Return _Item.PictureView
        End Get
    End Property
    Public Overloads Property Item As Marmat.Forms.PictureView.PictureItem

    Friend Sub New(image As Marmat.Forms.PictureView.PictureItem)

        ' 此调用是设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。

        Me.Item = image
    End Sub

    ''' <summary>
    ''' 获取缩略图
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub LoadThumbnailImage()
        Dim FileName As String = Me.Item.LocalPath
        If FileName = "" AndAlso Me.Item.Id > "" Then
            FileName = My.Computer.FileSystem.CombinePath(Me.PictureView.ThumbnailPath, Me.Item.Id)
            If My.Computer.FileSystem.FileExists(FileName) = False Then
                FileName = My.Computer.FileSystem.CombinePath(Me.PictureView.AttachmentPath, Me.Item.Id)
            End If
        End If
        If My.Computer.FileSystem.FileExists(FileName) Then
            Using bmp As New Bitmap(FileName)
                Me.Image = bmp.Clone
            End Using
        ElseIf Me.PictureView.FileService IsNot Nothing AndAlso Me.Item.Id > "" Then
            'todo 此处待修复
            If TypeOf Me.PictureView.FileService Is Marmat.Common.IImageUriService Then
                Dim uriService = CType(Me.PictureView.FileService, Marmat.Common.IImageUriService)
                Dim Uri As Uri = uriService.GenerateThumbnailUri(Me.Item.Id, "")
                If Uri IsNot Nothing Then
                    Me.ImageLocation = Uri.ToString
                End If
            ElseIf TypeOf Me.PictureView.FileService Is Marmat.Common.IFileUriService Then
                Dim uriService = CType(Me.PictureView.FileService, Marmat.Common.IFileUriService)
                Dim Uri As Uri = uriService.GeneratePresignedUri(Me.Item.Id)
                If Uri IsNot Nothing Then
                    Me.ImageLocation = Uri.ToString
                End If
            End If
        End If

    End Sub

    ''' <summary>
    ''' 获取原图
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub LoadImage()
        If Me.PictureView IsNot Nothing AndAlso Me.PictureView.SelectedItem IsNot Me.Item Then Return
        Me.PictureView.PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
        Me.PictureView.PictureBox1.Image = Me.PictureView.PictureBox1.InitialImage.Clone
        Dim FileName As String = Me.PictureView.AttachmentPath & "\" & Item.Id
        If My.Computer.FileSystem.FileExists(FileName) Then
            Select Case Me.PictureView.CacheMode
                Case FileEdit.CacheModeEnum.File
                    Using fs As New Bitmap(FileName)
                        Me.PictureView.PictureBox1.Image = fs.Clone
                    End Using
                Case FileEdit.CacheModeEnum.Encryption
            End Select
        ElseIf Me.PictureView.FileService IsNot Nothing AndAlso Me.Item.Id > "" Then
            If TypeOf Me.PictureView.FileService Is Marmat.Common.IFileUriService Then
                Dim uriService = CType(Me.PictureView.FileService, Marmat.Common.IFileUriService)
                Dim Uri As Uri = uriService.GeneratePresignedUri(Me.Item.Id)
                If Uri IsNot Nothing Then
                    Me.PictureView.LoadAsync(Me.Item, Uri)
                    Exit Sub
                End If
            End If
        End If

        If ImageEquals(Me.PictureView.PictureBox1.Image, Me.PictureView.PictureBox1.InitialImage) Then
            Me.PictureView.PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
            Me.PictureView.PictureBox1.Image = Me.PictureView.PictureBox1.ErrorImage
            'PictureItem_LoadCompleted(Me, New System.ComponentModel.AsyncCompletedEventArgs(Nothing, False, Nothing))
        Else
            Me.PictureView.PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
        End If
    End Sub
    Friend Sub PictureBoxItem_Click(sender As Object, e As EventArgs) Handles MyBase.Click
        Me.PictureView.SelectedItem = Me.Item
    End Sub

    Friend Sub PictureBoxItem_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
        Using Pen As New Pen(Color.FromArgb(160, 160, 160), 1)
            If Me.PictureView.SelectedItem Is Me.Item Then
                Pen.Width = 2
                Pen.Color = Color.Red
            End If
            Dim Wth As Integer = Pen.Width
            e.Graphics.DrawRectangle(Pen, Pen.Width - 1, Pen.Width - 1, sender.Width - Wth, sender.Height - Wth)
        End Using
    End Sub

    Private Sub BasePictureItem_LoadCompleted(sender As Object, e As AsyncCompletedEventArgs) Handles MyBase.LoadCompleted
        If e.Cancelled = False AndAlso e.Error Is Nothing AndAlso Me.IsDisposed = False Then '如果正常下载的话
            Me.SizeMode = PictureBoxSizeMode.Zoom
            Dim Thumbnail As Image = Me.Image
            Dim Attachment As Image = Nothing
            If Thumbnail.Width > Me.PictureView.ThumbnailSize OrElse Image.Height > Me.PictureView.ThumbnailSize Then '如果下载的不是缩略图，则压缩
                Thumbnail = Me.PictureView.GetThumbnailImage(Thumbnail, Me.PictureView.ThumbnailSize)
                Attachment = Me.Image
            End If
            Me.Image = Thumbnail.Clone
            If My.Computer.FileSystem.FileExists(Me.PictureView.ThumbnailPath & "\" & Me.Item.Id) = False Then
                Thumbnail.Save(Me.PictureView.ThumbnailPath & "\" & Me.Item.Id)
            End If
            If Attachment IsNot Nothing AndAlso My.Computer.FileSystem.FileExists(Me.PictureView.AttachmentPath & "\" & Me.Item.Id) = False Then
                Attachment.Save(Me.PictureView.AttachmentPath & "\" & Me.Item.Id)
            End If
        Else
            Me.SizeMode = PictureBoxSizeMode.CenterImage
            Me.Image = Me.ErrorImage.Clone
            If sender Is Me.PictureView.SelectedItem Then
                Me.PictureView.PictureBox1.SizeMode = PictureBoxSizeMode.CenterImage
                Me.PictureView.PictureBox1.Image = Me.PictureView.PictureBox1.ErrorImage.Clone
            End If
        End If
    End Sub

    Private Function ImageEquals(ByVal Img1 As Image, ByVal Img2 As Image) As Boolean
        Dim m1 As New MemoryStream()
        Dim m2 As New MemoryStream()
        Img1.Save(m1, Img1.RawFormat)
        Img2.Save(m2, Img1.RawFormat)
        If m1.Length <> m2.Length Then
            Return False
        End If
        '长度都不等了
        For i As Integer = 0 To m1.Length - 1
            If m1.ReadByte() <> m2.ReadByte() Then
                Return False
                '找到不同的了
            End If
        Next
        Return True
        '相同
    End Function

    Private Sub PictureBox_LoadCompleted(sender As Object, e As AsyncCompletedEventArgs) Handles PictureBox.LoadCompleted

    End Sub
End Class
